﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OnlineTestDBHELPER;
using OnlineTestModel;

namespace OnlineTestDAL
{
	public class PartsDAL
	{
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return DbHelperSQL.GetMaxID("id", "parts");
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) from parts");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(), parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Parts model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into parts(");
			strSql.Append("partname,partnumber,courseid,isimportant,parentid)");
			strSql.Append(" values (");
			strSql.Append("@partname,@partnumber,@courseid,@isimportant,@parentid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@partname", model.PartName),
					new SqlParameter("@partnumber", model.PartNumber),
					new SqlParameter("@courseid", model.CourseId),
					new SqlParameter("@isimportant", model.IsImportant),
					new SqlParameter("@parentid", model.ParentId)};
			

			object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Parts model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update parts set ");
			strSql.Append("partname=@partname,");
			strSql.Append("partnumber=@partnumber,");
			strSql.Append("courseid=@courseid,");
			strSql.Append("isimportant=@isimportant");
            strSql.Append("parentid=@parentid");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@partname", model.PartName),
					new SqlParameter("@partnumber", model.PartNumber),
					new SqlParameter("@courseid", model.CourseId),
					new SqlParameter("@isimportant", model.IsImportant),
                    new SqlParameter("@parentid", model.ParentId),
					new SqlParameter("@id", model.Id)};
			

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{

			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from parts ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from parts ");
			strSql.Append(" where id in (" + idlist + ")  ");
			int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Parts GetModel(int id)
		{

			StringBuilder strSql = new StringBuilder();
			strSql.Append("select  top 1 id,partname,partnumber,courseid,isimportant,parentid from parts ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			Parts model = new Parts();
			DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
			if (ds.Tables[0].Rows.Count > 0)
			{
				if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
				{
					model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if (ds.Tables[0].Rows[0]["partname"] != null && ds.Tables[0].Rows[0]["partname"].ToString() != "")
				{
					model.PartName = ds.Tables[0].Rows[0]["partname"].ToString();
				}
				if (ds.Tables[0].Rows[0]["partnumber"] != null && ds.Tables[0].Rows[0]["partnumber"].ToString() != "")
				{
					model.PartNumber = int.Parse(ds.Tables[0].Rows[0]["partnumber"].ToString());
				}
				if (ds.Tables[0].Rows[0]["courseid"] != null && ds.Tables[0].Rows[0]["courseid"].ToString() != "")
				{
					model.CourseId = int.Parse(ds.Tables[0].Rows[0]["courseid"].ToString());
				}
                if (ds.Tables[0].Rows[0]["parentid"] != null && ds.Tables[0].Rows[0]["parentid"].ToString() != "")
                {
                    model.CourseId = int.Parse(ds.Tables[0].Rows[0]["parentid"].ToString());
                }
				if (ds.Tables[0].Rows[0]["isimportant"] != null && ds.Tables[0].Rows[0]["isimportant"].ToString() != "")
				{
					if ((ds.Tables[0].Rows[0]["isimportant"].ToString() == "1") || (ds.Tables[0].Rows[0]["isimportant"].ToString().ToLower() == "true"))
					{
						model.IsImportant = true;
					}
					else
					{
						model.IsImportant = false;
					}
				}
				return model;
			}
			else
			{
				return null;
			}
		}
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select id,partname,partnumber,courseid,isimportant,parentid ");
			strSql.Append(" FROM parts ");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select ");
			if (Top > 0)
			{
				strSql.Append(" top " + Top.ToString());
			}
			strSql.Append(" id,partname,partnumber,courseid,isimportant,parentid ");
			strSql.Append(" FROM parts ");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) FROM parts ");
			if (strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby);
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from parts T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		

		#endregion  Method
	}
}
